package crawler.client.core.handle;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import crawler.client.core.configure.ClientCoreConfigure;
import crawler.client.sdk.expand.ip.GetIPAddress;
import crawler.protocol.client.ClientInfoProtocol;

@Service
public class InfoHandle {

	private static Logger log = LoggerFactory.getLogger(InfoHandle.class);

	private int urlCount;
	private String clientID;
	private String clientGroup;

	private ClientCoreConfigure clientCoreConfigure;

	@Autowired
	public InfoHandle(ClientCoreConfigure clientCoreConfigure, IDHandle idHandle,ClientGroupHandle  clientGroupHandle) {
		this.clientCoreConfigure = clientCoreConfigure;

		this.urlCount = Integer.parseInt(clientCoreConfigure.getUrlCount());
		this.clientID = idHandle.getClientId();
		this.clientGroup = clientGroupHandle.fetchClientGroup();
	}

	public String collectClientInfo() {
		ClientInfoProtocol clientInfo = new ClientInfoProtocol();

		Properties props = System.getProperties(); // 获得系统属性集
		String osName = props.getProperty("os.name"); // 操作系统名称
		String osArch = props.getProperty("os.arch"); // 操作系统构架
		String osVersion = props.getProperty("os.version"); // 操作系统版本

		String publicIp = GetIPAddress.getClientIp();
		if (publicIp != null && !"".equals(publicIp)) {
			clientInfo.setPublicIp(publicIp);
		}

		getLocalInfo(clientInfo);

		clientInfo.setClientId(clientID);
		clientInfo.setOsName(osName);
		clientInfo.setOsArch(osArch);
		clientInfo.setOsVersion(osVersion);
		clientInfo.setUrlCount(urlCount);
		clientInfo.setClientGroup(clientGroup);

		return JSON.toJSONString(clientInfo);
	}

	public void collectClientInfo(ClientInfoProtocol client) {
		collectClientInfo(client, false);
	}

	public void collectMinClientInfo(ClientInfoProtocol client) {
		getLocalInfo(client);
		client.setClientId(clientID);
		client.setUrlCount(urlCount);
		client.setClientGroup(clientGroup);
	}

	public void collectClientInfo(ClientInfoProtocol client, boolean wantPublicIp) {

		Properties props = System.getProperties(); // 获得系统属性集
		String osName = props.getProperty("os.name"); // 操作系统名称
		String osArch = props.getProperty("os.arch"); // 操作系统构架
		String osVersion = props.getProperty("os.version"); // 操作系统版本

		if (wantPublicIp) {
			String publicIp = GetIPAddress.getClientIp();
			if (publicIp != null && !"".equals(publicIp)) {
				client.setPublicIp(publicIp);
			}
		}

		getLocalInfo(client);

		client.setClientId(clientID);
		client.setOsName(osName);
		client.setOsArch(osArch);
		client.setOsVersion(osVersion);
		client.setUrlCount(urlCount);
		client.setClientGroup(clientGroup);
	}

	private void getLocalInfo(ClientInfoProtocol clientInfo) {
		try {
			InetAddress inetAddress = InetAddress.getLocalHost();

			String clientName = inetAddress.getHostName();
			String innerIp = inetAddress.getHostAddress();

			clientInfo.setClientName(clientName);
			clientInfo.setInnerIp(innerIp);

		} catch (UnknownHostException e) {
			log.error("Get Host Error", e);
			e.printStackTrace();
		}
	}
}
